package _2022.hot100._14_最长公共前缀;

/**
 * @author： YHSimon
 * @date： 2022-08-11 23:25
 */
public class Solution {
    public String longestCommonPrefix(String[] strs) {
        String res = strs[0];
        int len = strs.length;
        for (int i = 1; i < len; i++) {
            StringBuilder sb = new StringBuilder();
            int minLen=Math.min(res.length(), strs[i].length());
            int j=0;
            while(j<minLen&&res.charAt(j)==strs[i].charAt(j)){
                sb.append(res.charAt(j));
                j++;
            }
            res=sb.toString();
        }
        return res;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.longestCommonPrefix(new String[]{"flower","flow","flight"}));
        System.out.println(s.longestCommonPrefix(new String[]{"ab", "a"}));
    }
}
